package com.ruoyi.api.controller;

import com.ruoyi.api.service.LocationService;
import com.ruoyi.api.service.VimUserApiService;
import com.ruoyi.api.utils.SysMath;
import com.ruoyi.api.vo.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 轨迹回放
 */
@RestController
@RequestMapping("/api/sys/location")
public class LocationsController {

    @Autowired
    private LocationService locationService;

    @Autowired
    private VimUserApiService vimUserApiService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 上传坐标以及轨迹
     * @param locations
     * @return
     */
    @PostMapping("add")
    public AjaxResult addLocation(@RequestBody Locations locations){
        locationService.add(locations);
        return AjaxResult.success();
    }

    /**
     * 上传离线坐标及轨迹
     * @param list
     * @return
     */
    @PostMapping("addOffline")
    public AjaxResult addOffline(@RequestBody List<Locations> list){
        if(list.isEmpty()){
            return AjaxResult.error("上传坐标为空");
        }
        try{
            locationService.addOffline(list);
        }catch (Exception e){
            return AjaxResult.error("上传失败");
        }
        return AjaxResult.success("上传成功");
    }

    /**
     * 根据路径查询坐标信息
     * @param trackId
     * @return
     */
    @PostMapping("selectlocation")
    public AjaxResult selectlocation(@RequestBody Object trackId){
        List<Locations> location = locationService.selectlocation(String.valueOf(trackId));
        return AjaxResult.success(location);
    }

    /**
     * 根据用户id查询用户路径
     * @param userId
     * @return
     */
    @PostMapping("selectpath")
    public AjaxResult selectPath(@RequestBody String userId){
//        Map<String,List<String>> listMap = new HashMap<>();
//        List<String> lists = new ArrayList<>();
        Date time = new Date();
        LocationDeom locationDeom = new LocationDeom();
        List<String> locations = locationService.selectPath(userId,time);
//        for (int i = 0; i < locations.size(); i++) {
//            List<Locations> location = locationService.selectlocation(locations.get(i));
//            if(lists.size() == 0){
//                lists.add(location.get(0).getLocation());
//                lists.add(location.get(location.size()-1).getLocation());
//            }
//            lists.set(0,location.get(0).getLocation());
//            lists.set(1,location.get(location.size()-1).getLocation());
//            listMap.put(locations.get(i),lists);
//        }
        locationDeom.setList(locations);
        return AjaxResult.success(locationDeom);
    }

    /**
     * 根据路径id删除用户路径
     * @param trackId
     * @return
     */
    @DeleteMapping("deletepath")
    public AjaxResult delectPath(@RequestParam String trackId){
        boolean a = locationService.deletepath(trackId);
        if(a){
            return AjaxResult.success("删除成功!");
        }else {
            return AjaxResult.error("删除失败!");
        }
    }

    /**
     * 查询用户最新上传位置信息
     * @param userId
     * @return
     */
    @PostMapping("selectLocation")
    public AjaxResult selectLocation(@RequestParam String userId){
        String location = locationService.selectUserLocation(userId);
        return AjaxResult.success(location);
    }

    /**
     * 查询车辆预约用户在某段时间内的使用情况
     * @param approval
     * @return
     */
    @PostMapping("selectCar")
    public AjaxResult selectCar(@RequestBody Approval approval){
        List<ApprovalDemo> list = locationService.selectByCar(approval);
        if(list.isEmpty()){
            return AjaxResult.error("该车辆使用时未上传位置数据");
        }
        return AjaxResult.success(list);
    }

    @PostMapping("selectByTrack")
    public AjaxResult selectByTrack(@RequestParam String trackId){
        List<String> list = locationService.selectByTrack(trackId);
        return AjaxResult.success(list);
    }

    /**
     * 没啥用,测试提供假数据
     * @return
     */
    @GetMapping("beginLocation")
    public AjaxResult beginLocation(){
        List<String> q = new ArrayList<>();
        List<Long> a = vimUserApiService.getAllUserId();
        for (Long aLong : a) {
            q.add(String.valueOf(aLong));
        }
        for (String s : q) {
            redisCache.setCacheObject("locationTest:" + "locationTest" + s,"116.20,39.56");
//            jedis.hset("locationTest","locationTest" + s,"116.20,39.56");
        }
        return AjaxResult.success();
    }

    /**
     * 重复调用，自定义坐标，用于测试
     * @return
     */
    @GetMapping("repeat")
    public AjaxResult insertRepeat(){
        List<String> q = new ArrayList<>();
        List<Long> t = vimUserApiService.getAllUserId();
        for (Long aLong : t) {
            q.add(String.valueOf(aLong));
        }
        Date date = new Date();
//        Jedis jedis = new Jedis();
        for (String s : q) {
//            String b = jedis.get("locationTest" + s);
            Object w = redisCache.getCacheObject("locationTest:" + "locationTest" + s);
            String b = String.valueOf(w);
            double a = randomLonLat()/10000;
            double h = randomLonLat()/10000;
            String[] c = b.split(",");
            Double lon = Double.valueOf(c[0]);
            Double lin = Double.valueOf(c[1]);
            Double e = lon + a;
            Double f = lin + h;
            String g = e + "," + f;
            Locations locations = new Locations();
            locations.setUserId(s);
            locations.setTime(date);
            locations.setLocation(g);
            locationService.add(locations);
//            jedis.set("locationTest" + s,g);
            redisCache.setCacheObject("locationTest:" + "locationTest" + s,g);
        }
        return AjaxResult.success();
    }

    public Double randomLonLat() {
//        Random random = new Random();
        int a  = (int)(Math.random()*11) - 5;
        Double b = Double.valueOf(a);
        return b;
    }

    /**
     * 测试算法
     * @return
     */
    @GetMapping("test")
    public AjaxResult testMath(){
//        List<String> q = new ArrayList<>();
//        List<Long> t = vimUserApiService.getAllUserId();
//        List<String> list = new ArrayList<>();
//        for (Long aLong : t) {
//            q.add(String.valueOf(aLong));
//        }
//        for (String s : q) {
//             list.add(locationService.selectUserLocation(s));
//        }
        List<String> list = new ArrayList<>();
        list.add("2,0");
        list.add("4,0");
        list.add("6,0");
        list.add("10,0");
        list.add("-6,0");
        Circle circle = SysMath.mathCircle(list);
        return AjaxResult.success(circle);
    }
}
